import requests
from bs4 import BeautifulSoup
import pandas as pd
import os
import re
import tkinter as tk
from tkinter import messagebox, scrolledtext
from datetime import datetime
import json
import random


class LotteryCrawler:
    def __init__(self):
        self.data_file = "dlt_history.csv"
        self.columns = ["期号", "开奖日期", "前区号码", "后区号码"]

    def get_latest_period(self):
        """获取本地数据中最新的期号"""
        if os.path.exists(self.data_file):
            try:
                df = pd.read_csv(self.data_file)
                if len(df) > 0:
                    # 按期号排序，获取最新期号
                    df_sorted = df.sort_values('期号', ascending=False)
                    return df_sorted.iloc[0]['期号']
            except:
                pass
        return None

    def crawl_history_data(self):
        """从指定网页爬取历史数据，只获取新数据"""
        all_data = []

        # 目标网页URL
        url = "https://datachart.500.com/dlt/history/newinc/history.php?start=001&end=99999"

        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
                "Referer": "https://datachart.500.com/dlt/",
            }

            print("正在访问网页...")
            response = requests.get(url, headers=headers, timeout=15)
            response.encoding = 'gb2312'

            # 获取本地最新期号
            latest_local_period = self.get_latest_period()
            print(f"本地最新期号: {latest_local_period}")

            # 解析HTML
            soup = BeautifulSoup(response.text, 'html.parser')

            """备用方法解析数据"""
            all_data = []
            soup = BeautifulSoup(response.text, 'html.parser')

            # 查找所有tr元素
            tr_elements = soup.find_all('tr')
            new_data_count = 0
            found_latest = False
            for tr in tr_elements:
                # 跳过表头
                if tr.find('th'):
                    continue

                cols = tr.find_all('td')
                if len(cols) >= 8:  # 至少需要8列数据
                    try:
                        # 期号
                        period = cols[0].text.strip()

                        # 如果已经有本地数据，且遇到已存在的期号，跳过
                        if latest_local_period and period == latest_local_period:
                            found_latest = True
                            continue

                        # 如果已经找到最新期号，但后面又出现了更新的数据，继续获取
                        if found_latest and latest_local_period:
                            # 比较期号，确保只获取更新的数据
                            if int(period) <= int(latest_local_period):
                                continue

                        # 前区号码 (第2-6列)
                        front_nums = []
                        for i in range(1, 6):
                            num = cols[i].text.strip()
                            if num.isdigit():
                                front_nums.append(num.zfill(2))

                        # 后区号码 (第7-8列)
                        back_nums = []
                        for i in range(6, 8):
                            num = cols[i].text.strip()
                            if num.isdigit():
                                back_nums.append(num.zfill(2))

                        # 开奖日期 (最后一列)
                        date = cols[-1].text.strip()

                        if len(front_nums) == 5 and len(back_nums) == 2:
                            front_str = " ".join(front_nums)
                            back_str = " ".join(back_nums)
                            all_data.append([period, date, front_str, back_str])

                            new_data_count += 1
                    except Exception as e:
                        print(f"解析行数据时出错: {e}")
                    continue
                print(f"从页面解析到{new_data_count}期新数据")

            # 如果没有找到新数据，检查是否需要更新
            if not all_data:
                if latest_local_period:
                    return "已是最新数据，无需更新"
                else:
                    # 如果没有本地数据，使用模拟数据
                    return self.use_sample_data()
            else:
                return self.save_data(all_data)
        except Exception as e:
            print(f"爬取数据时出错: {e}")
            return f"爬取数据时出错: {e}"

        # 保存数据
        if all_data:
            return self.save_data(all_data)
        else:
            return "未获取到新数据"

    def crawl_all_data(self):
        """从指定网页爬取历史数据"""
        all_data = []

        # 目标网页URL
        url = "https://datachart.500.com/dlt/history/newinc/history.php?start=001&end=99999"

        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
                "Referer": "https://datachart.500.com/dlt/",
            }

            print("正在访问网页...")
            response = requests.get(url, headers=headers, timeout=15)
            response.encoding = 'gb2312'  # 这个网站使用gb2312编码

            # 解析HTML
            soup = BeautifulSoup(response.text, 'html.parser')

            # 查找数据表格 - 根据您提供的表格结构
            table = soup.find('table', {'id': 'tdata'})
            if not table:
                table = soup.find('table', {'class': 'tchart'})

            if table:
                print("找到数据表格")
                rows = table.find_all('tr')

                for row in rows:
                    # 跳过表头行
                    if row.find('th'):
                        continue

                    cols = row.find_all('td')
                    if len(cols) >= 15:  # 根据您提供的列数，至少有15列
                        # 期号 - 第1列
                        period = cols[0].text.strip()

                        # 前区号码 - 第2-6列
                        front_nums = []
                        for i in range(1, 6):  # 2-6列对应索引1-5
                            num = cols[i].text.strip().zfill(2)
                            front_nums.append(num)

                        # 后区号码 - 第7-8列
                        back_nums = []
                        for i in range(6, 8):  # 7-8列对应索引6-7
                            num = cols[i].text.strip().zfill(2)
                            back_nums.append(num)

                        # 开奖日期 - 最后一列
                        date = cols[-1].text.strip()

                        if len(front_nums) == 5 and len(back_nums) == 2:
                            front_str = " ".join(front_nums)
                            back_str = " ".join(back_nums)
                            all_data.append([period, date, front_str, back_str])

                print(f"从页面解析到{len(all_data)}期数据")

            # 如果表格查找失败，尝试备用方法
            if not all_data:
                print("尝试备用方法...")
                all_data = self.crawl_backup_data(response.text)

        except Exception as e:
            print(f"爬取数据时出错: {e}")
            return f"爬取数据时出错: {e}"

        # 保存数据
        if all_data:
            return self.save_data(all_data)
        else:
            return "未获取到任何数据，使用模拟数据演示"

    def crawl_backup_data(self, html_content):
        """备用方法解析数据"""
        all_data = []
        soup = BeautifulSoup(html_content, 'html.parser')

        # 查找所有tr元素
        tr_elements = soup.find_all('tr')
        for tr in tr_elements:
            # 跳过表头
            if tr.find('th'):
                continue

            cols = tr.find_all('td')
            if len(cols) >= 8:  # 至少需要8列数据
                try:
                    # 期号
                    period = cols[0].text.strip()

                    # 前区号码 (第2-6列)
                    front_nums = []
                    for i in range(1, 6):
                        num = cols[i].text.strip()
                        if num.isdigit():
                            front_nums.append(num.zfill(2))

                    # 后区号码 (第7-8列)
                    back_nums = []
                    for i in range(6, 8):
                        num = cols[i].text.strip()
                        if num.isdigit():
                            back_nums.append(num.zfill(2))

                    # 开奖日期 (最后一列)
                    date = cols[-1].text.strip()

                    if len(front_nums) == 5 and len(back_nums) == 2:
                        front_str = " ".join(front_nums)
                        back_str = " ".join(back_nums)
                        all_data.append([period, date, front_str, back_str])

                except Exception as e:
                    print(f"解析行数据时出错: {e}")
                    continue

        print(f"备用方法找到{len(all_data)}期数据")
        return all_data

    def get_recent_30_data(self):
        """获取最近30期数据"""
        if not os.path.exists(self.data_file):
            return None

        try:
            df = pd.read_csv(self.data_file)
            if len(df) >= 100:
                return df.head(100)  # 因为数据是按从新到旧排序的
            else:
                return df
        except:
            return None

    def analyze_with_ai(self, api_type="deepseek"):
        """使用AI分析最近30期数据"""
        recent_data = self.get_recent_30_data()
        if recent_data is None or len(recent_data) == 0:
            return "没有足够的数据进行分析"

        # 准备分析数据
        #analysis_data = []
        #for _, row in recent_data.iterrows():
        #    analysis_data.append({
        #        "期号": row["期号"],
        #        "开奖日期": row["开奖日期"],
        #        "前区": row["前区号码"],
        #        "后区": row["后区号码"]
        #    })
        analysis_data = []
        for _, row in recent_data.iterrows():
            analysis_data.append({
                "期号": row["期号"],
                "开奖日期": row["开奖日期"],
                "前区": row["前区号码"],
                "后区": row["后区号码"]
            })
        # 构建AI提示词
        prompt = self.build_ai_prompt(analysis_data)
        #print(analysis_data)
        #return prompt
        if api_type == "deepseek":
            return self.call_deepseek_api(prompt)
        else:
            return self.call_douban_api(prompt)

    def build_ai_prompt(self, data):
        """构建AI分析提示词"""
        prompt = """你是一个数据分析专家和推理专家，请分析以下100组数据，找出数字出现的规律，包括不限于冷热号、连号、计算类型等情况，
        分析结果预测下一组号码组合。请给出详细的分析过程和号码。

        历史数据：
        """

        for item in data:
            prompt += f" { {item['前区']} , {item['后区']} }\n"
            #prompt += f"期号{item['期号']} ({item['开奖日期']}): 前区 {item['前区']} | 后区 {item['后区']}\n"

        prompt += """
        
        #请分析：
        #1. 前区号码出现频率分析（热号、温号、冷号）
        #2. 后区号码出现频率分析
        #3. 连号规律分析
        #4. 奇偶比分析
        #5. 大小号分布（前区1-17为小，18-35为大；后区1-6为小，7-12为大）
        #6. 基于以上分析，推荐3组最可能的号码组合"""

        return prompt

    def call_deepseek_api(self, prompt):
        """调用DeepSeek API"""
        try:
            # 这里需要替换为实际的DeepSeek API密钥和端点
            # 由于API调用需要密钥，这里提供模拟响应
            return self.simulate_ai_response("DeepSeek")
        except Exception as e:
            return f"DeepSeek API调用失败: {e}"

    def call_douban_api(self, prompt):
        """调用豆包API"""
        try:
            # 这里需要替换为实际的豆包API密钥和端点
            # 由于API调用需要密钥，这里提供模拟响应
            return self.simulate_ai_response("豆包")
        except Exception as e:
            return f"豆包API调用失败: {e}"

    def simulate_ai_response(self, ai_type):
        """模拟AI响应（实际使用时替换为真实API调用）"""
        # 基于简单规则生成推荐号码
        recent_data = self.get_recent_30_data()
        if recent_data is None:
            return "数据不足，无法分析"

        # 简单分析：找出热门号码
        front_count = {}
        back_count = {}

        for _, row in recent_data.iterrows():
            front_nums = row["前区号码"].split()
            back_nums = row["后区号码"].split()

            for num in front_nums:
                front_count[num] = front_count.get(num, 0) + 1
            for num in back_nums:
                back_count[num] = back_count.get(num, 0) + 1

        # 获取热门号码
        hot_front = sorted(front_count.items(), key=lambda x: x[1], reverse=True)[:35]
        hot_back = sorted(back_count.items(), key=lambda x: x[1], reverse=True)[:12]

        # 生成3组推荐号码
        recommendations = []
        for i in range(3):
            # 从前10个热门号码中随机选5个
            front_candidates = [num for num, count in hot_front]
            selected_front = sorted(random.sample(front_candidates, 5))

            # 从前6个热门后区号码中随机选2个
            back_candidates = [num for num, count in hot_back]
            selected_back = sorted(random.sample(back_candidates, 2))

            recommendations.append({
                "前区": " ".join(selected_front),
                "后区": " ".join(selected_back)
            })

        # 构建响应
        response = f"""【{ai_type}AI分析报告】\n\n"""
        response += "基于最近30期数据分析：\n\n"

        response += "📊 前区热门号码（出现次数）：\n"
        for num, count in hot_front:
            response += f"{num}: {count}次  "
        response += "\n\n"

        response += "📊 后区热门号码（出现次数）：\n"
        for num, count in hot_back:
            response += f"{num}: {count}次  "
        response += "\n\n"

        response += "🎯 推荐号码组合：\n"
        for i, rec in enumerate(recommendations, 1):
            response += f"第{i}组: 前区 {rec['前区']} | 后区 {rec['后区']}\n"

        response += "\n💡 温馨提示：彩票分析仅供参考，请理性购彩！"

        return response

    def save_data(self, all_data):
        """保存数据到文件"""
        if all_data:
            df = pd.DataFrame(all_data, columns=self.columns)

            if os.path.exists(self.data_file):
                # 读取现有数据
                existing_df = pd.read_csv(self.data_file)
                # 合并数据并去重
                combined_df = pd.concat([existing_df, df], ignore_index=True)
                combined_df.drop_duplicates(subset=["期号"], keep='first', inplace=True)
                combined_df.sort_values(by="期号", ascending=False, inplace=True)
                combined_df.to_csv(self.data_file, index=False)
                added_count = len(combined_df) - len(existing_df)
                result = f"数据已更新，新增{added_count}期，共{len(combined_df)}期"
            else:
                df.sort_values(by="期号", ascending=False, inplace=True)
                df.to_csv(self.data_file, index=False)
                result = f"数据已保存到{self.data_file}，共{len(all_data)}期"

            print(result)
            return result
        else:
            return "未获取到数据"

    def use_sample_data(self):
        """使用模拟数据进行演示"""
        print("使用模拟数据进行演示")
        all_data = [
            ["25104", "2025-09-10", "02 06 09 22 34", "02 08"],
            ["25103", "2025-09-07", "05 12 18 25 31", "03 07"],
            ["25102", "2025-09-03", "08 15 22 29 35", "01 06"],
        ]

        df = pd.DataFrame(all_data, columns=self.columns)
        df.to_csv(self.data_file, index=False)
        return f"使用模拟数据，共{len(all_data)}期"

    def check_numbers(self, numbers):
        """检查号码是否在历史数据中存在"""
        if not os.path.exists(self.data_file):
            return "数据文件不存在，请先获取数据"

        try:
            df = pd.read_csv(self.data_file)
        except:
            return "数据文件读取失败"

        if len(df) == 0:
            return "数据文件为空"

        # 解析用户输入的号码
        try:
            # 支持多种输入格式
            if '|' in numbers:
                front_str, back_str = numbers.split('|')
                front_nums = [x.strip().zfill(2) for x in re.findall(r'\d+', front_str)]
                back_nums = [x.strip().zfill(2) for x in re.findall(r'\d+', back_str)]
            else:
                all_nums = [x.zfill(2) for x in re.findall(r'\d+', numbers)]
                if len(all_nums) != 7:
                    return "请输入7个数字(5个前区+2个后区)"
                front_nums = all_nums[:5]
                back_nums = all_nums[5:]

            # 验证号码范围
            for num in front_nums:
                if int(num) < 1 or int(num) > 35:
                    return f"前区号码{num}超出范围(1-35)"
            for num in back_nums:
                if int(num) < 1 or int(num) > 12:
                    return f"后区号码{num}超出范围(1-12)"

            front_nums.sort()
            back_nums.sort()

            # 转换为字符串格式用于比较
            user_front_str = " ".join(front_nums)
            user_back_str = " ".join(back_nums)

        except Exception as e:
            return f"输入格式错误: {e}"

        # 在历史数据中查找匹配的号码
        results = []
        for _, row in df.iterrows():
            # 检查是否完全匹配
            if user_front_str == row["前区号码"] and user_back_str == row["后区号码"]:
                results.append(f"🎯 完全匹配: 第{row['期号']}期 ({row['开奖日期']})")
                continue

            # 检查匹配情况
            history_front = row["前区号码"].split()
            history_back = row["后区号码"].split()

            front_match = len(set(front_nums) & set(history_front))
            back_match = len(set(back_nums) & set(history_back))

            if front_match >= 2 and back_match >= 1:
                match_level = self.get_match_level(front_match, back_match)
                results.append(
                    f"{match_level} 第{row['期号']}期 ({row['开奖日期']}): 前区{front_match}个, 后区{back_match}个")
            elif front_match >= 1 and back_match >= 2:
                match_level = self.get_match_level(front_match, back_match)
                results.append(
                    f"{match_level} 第{row['期号']}期 ({row['开奖日期']}): 前区{front_match}个, 后区{back_match}个")
            elif back_match >= 2:
                match_level = self.get_match_level(front_match, back_match)
                results.append(
                    f"{match_level} 第{row['期号']}期 ({row['开奖日期']}): 前区{front_match}个, 后区{back_match}个")
            elif front_match >= 3:
                match_level = self.get_match_level(front_match, back_match)
                results.append(
                    f"{match_level} 第{row['期号']}期 ({row['开奖日期']}): 前区{front_match}个, 后区{back_match}个")

        if results:
            return "\n".join(results[:])
        else:
            return "未在历史数据中找到匹配的号码"

    def get_match_level(self, front_match, back_match):
        """根据匹配情况返回中奖等级"""
        if front_match == 5 and back_match == 2:
            return "🔥 一等奖"
        elif front_match == 5 and back_match == 1:
            return "🔥 二等奖"
        elif front_match == 5 and back_match == 0:
            return "🔥 三等奖"
        elif front_match == 4 and back_match == 2:
            return "⭐ 四等奖"
        elif front_match == 4 and back_match == 1:
            return "⭐ 五等奖"
        elif front_match == 3 and back_match == 2:
            return "⭐ 六等奖"
        elif front_match == 4 and back_match == 0:
            return "⭐ 七等奖"
        elif front_match == 3 and back_match == 1:
            return "✅ 八等奖"
        elif front_match == 2 and back_match == 2:
            return "✅ 八等奖"
        elif front_match == 3 and back_match == 0:
            return "✅ 九等奖"
        elif front_match == 1 and back_match == 2:
            return "✅ 九等奖"
        elif front_match == 2 and back_match == 1:
            return "✅ 九等奖"
        elif front_match == 0 and back_match == 2:
            return "✅ 九等奖"
        else:
            return "📊 匹配"

    def get_latest_data(self):
        """获取最新一期开奖数据"""
        if not os.path.exists(self.data_file):
            return "数据文件不存在"

        try:
            df = pd.read_csv(self.data_file)
            if len(df) == 0:
                return "数据文件为空"

            latest = df.iloc[0]  # 第一行是最新的

            result = f"最新一期: 第{latest['期号']}期 ({latest['开奖日期']})\n"
            result += f"前区: {latest['前区号码']}\n"
            result += f"后区: {latest['后区号码']}"

            return result
        except:
            return "数据文件读取失败"



class LotteryGUI:
    def __init__(self):
        self.crawler = LotteryCrawler()
        self.window = tk.Tk()
        self.window.title("体彩大乐透历史数据查询与AI分析")
        self.window.geometry("900x700")

        self.setup_ui()
        # 程序启动时自动检查更新
        self.auto_update()

    def setup_ui(self):
        # 标题
        title_label = tk.Label(self.window, text="体彩大乐透历史数据查询与AI分析系统",
                               font=("Arial", 16, "bold"), fg="blue")
        title_label.pack(pady=10)

        # 按钮框架 - 第一行
        button_frame1 = tk.Frame(self.window)
        button_frame1.pack(pady=5)

        # 获取所有数据按钮
        self.crawl_all_button = tk.Button(button_frame1, text="获取所有数据",
                                          command=self.crawl_all_data, width=12, bg="lightgreen")
        self.crawl_all_button.pack(side=tk.LEFT, padx=3)

        # 自动更新按钮
        self.update_button = tk.Button(button_frame1, text="检查更新",
                                       command=self.auto_update, width=12, bg="lightblue")
        self.update_button.pack(side=tk.LEFT, padx=3)

        # 最新一期按钮
        self.latest_button = tk.Button(button_frame1, text="显示最新一期",
                                       command=self.show_latest, width=12, bg="lightyellow")
        self.latest_button.pack(side=tk.LEFT, padx=3)

        # 按钮框架 - 第二行 (AI分析按钮)
        button_frame2 = tk.Frame(self.window)
        button_frame2.pack(pady=5)

        # DeepSeek分析按钮
        self.deepseek_button = tk.Button(button_frame2, text="DeepSeek分析",
                                         command=lambda: self.ai_analysis("deepseek"),
                                         width=12, bg="#FF6B6B", fg="white")
        self.deepseek_button.pack(side=tk.LEFT, padx=3)

        # 豆包分析按钮
        self.douban_button = tk.Button(button_frame2, text="豆包分析",
                                       command=lambda: self.ai_analysis("douban"),
                                       width=12, bg="#4ECDC4", fg="white")
        self.douban_button.pack(side=tk.LEFT, padx=3)

        # 清空结果按钮
        self.clear_button = tk.Button(button_frame2, text="清空结果",
                                      command=self.clear_results, width=12, bg="lightcoral")
        self.clear_button.pack(side=tk.LEFT, padx=3)

        # 输入区域
        input_frame = tk.Frame(self.window)
        input_frame.pack(pady=10, padx=20, fill=tk.X)

        self.input_label = tk.Label(input_frame, text="输入号码:", font=("Arial", 12))
        self.input_label.pack(anchor=tk.W)

        # 号码输入框
        self.input_entry = tk.Entry(input_frame, width=60, font=("Arial", 11))
        self.input_entry.pack(pady=5, fill=tk.X)
        self.input_entry.insert(0, "02 06 09 22 34 02 08")

        # 格式提示
        format_label = tk.Label(input_frame, text="格式: 01 02 03 04 05 06 07 或 01 02 03 04 05 | 06 07",
                                fg="gray", font=("Arial", 9))
        format_label.pack(anchor=tk.W)

        # 查询按钮
        self.check_button = tk.Button(self.window, text="查询号码",
                                      command=self.check_numbers, width=15,
                                      bg="orange", font=("Arial", 12, "bold"))
        self.check_button.pack(pady=10)

        # 结果显示区域
        result_frame = tk.Frame(self.window)
        result_frame.pack(pady=10, padx=20, fill=tk.BOTH, expand=True)

        result_label = tk.Label(result_frame, text="查询结果:", font=("Arial", 12, "bold"))
        result_label.pack(anchor=tk.W)

        self.result_text = scrolledtext.ScrolledText(result_frame, width=100, height=25,
                                                     font=("Consolas", 10))
        self.result_text.pack(fill=tk.BOTH, expand=True)

        # 状态栏
        self.status_var = tk.StringVar()
        self.status_var.set("就绪 - 点击'检查更新'获取最新数据")
        status_bar = tk.Label(self.window, textvariable=self.status_var, bd=1,
                              relief=tk.SUNKEN, anchor=tk.W, font=("Arial", 10))
        status_bar.pack(side=tk.BOTTOM, fill=tk.X)

        # 显示初始提示
        self.result_text.insert(tk.END, "欢迎使用体彩大乐透查询与AI分析系统！\n")
        self.result_text.insert(tk.END, "1. 点击'检查更新'获取最新开奖数据\n")
        self.result_text.insert(tk.END, "2. 使用'DeepSeek分析'或'豆包分析'进行AI预测\n")
        self.result_text.insert(tk.END, "3. 输入号码并点击'查询号码'进行历史比对\n\n")

    def ai_analysis(self, ai_type):
        """AI分析功能"""
        self.status_var.set(f"正在使用{ai_type}进行分析...")
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, f"正在使用{ai_type}分析最近30期数据...\n")
        self.window.update()

        result = self.crawler.analyze_with_ai(ai_type)
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, result)
        self.status_var.set(f"{ai_type}分析完成")

    def crawl_all_data(self):
        """获取所有数据"""
        self.status_var.set("正在获取所有历史数据...")
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, "正在获取所有历史数据...\n")
        self.window.update()

        # 这里需要实现获取所有数据的方法
        result = self.crawler.crawl_all_data()
        self.result_text.insert(tk.END, result + "\n")
        self.status_var.set("数据获取完成")

    def auto_update(self):
        """自动检查并更新数据"""
        self.status_var.set("正在自动检查更新...")
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, "正在自动检查数据更新...\n")
        self.window.update()

        result = self.crawler.crawl_history_data()
        self.result_text.insert(tk.END, result + "\n")
        self.status_var.set("自动更新完成")

    def show_latest(self):
        result = self.crawler.get_latest_data()
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, result)
        self.status_var.set("已显示最新一期数据")

    def check_numbers(self):
        numbers = self.input_entry.get()
        if not numbers:
            messagebox.showwarning("警告", "请输入号码")
            return

        self.status_var.set("正在查询号码...")
        self.window.update()

        result = self.crawler.check_numbers(numbers)
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, result)
        self.status_var.set("号码查询完成")

    def clear_results(self):
        self.result_text.delete(1.0, tk.END)
        self.status_var.set("已清空结果")

    def run(self):
        self.window.mainloop()


if __name__ == "__main__":
    # 启动GUI
    app = LotteryGUI()
    app.run()